package com.emath.objects;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import com.emath.shared.BigRational;
import com.emath.shared.NetUtils;

public class GameState {
	private int gameID;
	private String player1;
	private List<CurrentCard> p1playDeck = new ArrayList<CurrentCard>();
	private List<CurrentCard> p1hand = new ArrayList<CurrentCard>();
	private List<CurrentCard> p1discard = new ArrayList<CurrentCard>();
	private List<CurrentCard> p1stack1 = new ArrayList<CurrentCard>();
	private List<CurrentCard> p1stack2 = new ArrayList<CurrentCard>();
	private List<CurrentCard> p1stack3 = new ArrayList<CurrentCard>();
	private List<CurrentCard> p1stack4 = new ArrayList<CurrentCard>();
	private List<CurrentCard> p1stack5 = new ArrayList<CurrentCard>();
	private BigRational p1power;
	private BigRational p1dominance;
	private BigRational p1strength;
	private String player2;
	private List<CurrentCard> p2playDeck = new ArrayList<CurrentCard>();
	private List<CurrentCard> p2hand = new ArrayList<CurrentCard>();
	private List<CurrentCard> p2discard = new ArrayList<CurrentCard>();
	private List<CurrentCard> p2stack1 = new ArrayList<CurrentCard>();
	private List<CurrentCard> p2stack2 = new ArrayList<CurrentCard>();
	private List<CurrentCard> p2stack3 = new ArrayList<CurrentCard>();
	private List<CurrentCard> p2stack4 = new ArrayList<CurrentCard>();
	private List<CurrentCard> p2stack5 = new ArrayList<CurrentCard>();
	private BigRational p2power;
	private BigRational p2dominance;
	private BigRational p2strength;
	private String turn = "";
	private String round = "";
	private String objectState = "";
	public String getPlayer1() {
		return player1;
	}
	public void setPlayer1(String player1) {
		this.player1 = player1;
	}
	public int getGameID() {
		return gameID;
	}
	public void setGameID(int gameID) {
		this.gameID = gameID;
	}
	public List<CurrentCard> getP1playDeck() {
		return p1playDeck;
	}
	public void setP1playDeck(List<CurrentCard> p1playDeck) {
		this.p1playDeck = p1playDeck;
	}
	public List<CurrentCard> getP1hand() {
		return p1hand;
	}
	public void setP1hand(List<CurrentCard> p1hand) {
		this.p1hand = p1hand;
	}
	public List<CurrentCard> getP1discard() {
		return p1discard;
	}
	public void setP1discard(List<CurrentCard> p1discard) {
		this.p1discard = p1discard;
	}
	public List<CurrentCard> getP1stack1() {
		return p1stack1;
	}
	public void setP1stack1(List<CurrentCard> p1stack1) {
		this.p1stack1 = p1stack1;
	}
	public List<CurrentCard> getP1stack2() {
		return p1stack2;
	}
	public void setP1stack2(List<CurrentCard> p1stack2) {
		this.p1stack2 = p1stack2;
	}
	public List<CurrentCard> getP1stack3() {
		return p1stack3;
	}
	public void setP1stack3(List<CurrentCard> p1stack3) {
		this.p1stack3 = p1stack3;
	}
	public List<CurrentCard> getP1stack4() {
		return p1stack4;
	}
	public void setP1stack4(List<CurrentCard> p1stack4) {
		this.p1stack4 = p1stack4;
	}
	public List<CurrentCard> getP1stack5() {
		return p1stack5;
	}
	public void setP1stack5(List<CurrentCard> p1stack5) {
		this.p1stack5 = p1stack5;
	}
	public BigRational getP1power() {
		return p1power;
	}
	public void setP1power(BigRational p1power) {
		this.p1power = p1power;
	}
	public BigRational getP1dominance() {
		return p1dominance;
	}
	public void setP1dominance(BigRational p1dominance) {
		this.p1dominance = p1dominance;
	}
	public BigRational getP1strength() {
		return p1strength;
	}
	public void setP1strength(BigRational p1strength) {
		this.p1strength = p1strength;
	}
	public String getPlayer2() {
		return player2;
	}
	public void setPlayer2(String player2) {
		this.player2 = player2;
	}
	public List<CurrentCard> getP2playDeck() {
		return p2playDeck;
	}
	public void setP2playDeck(List<CurrentCard> p2playDeck) {
		this.p2playDeck = p2playDeck;
	}
	public List<CurrentCard> getP2hand() {
		return p2hand;
	}
	public void setP2hand(List<CurrentCard> p2hand) {
		this.p2hand = p2hand;
	}
	public List<CurrentCard> getP2discard() {
		return p2discard;
	}
	public void setP2discard(List<CurrentCard> p2discard) {
		this.p2discard = p2discard;
	}
	public List<CurrentCard> getP2stack1() {
		return p2stack1;
	}
	public void setP2stack1(List<CurrentCard> p2stack1) {
		this.p2stack1 = p2stack1;
	}
	public List<CurrentCard> getP2stack2() {
		return p2stack2;
	}
	public void setP2stack2(List<CurrentCard> p2stack2) {
		this.p2stack2 = p2stack2;
	}
	public List<CurrentCard> getP2stack3() {
		return p2stack3;
	}
	public void setP2stack3(List<CurrentCard> p2stack3) {
		this.p2stack3 = p2stack3;
	}
	public List<CurrentCard> getP2stack4() {
		return p2stack4;
	}
	public void setP2stack4(List<CurrentCard> p2stack4) {
		this.p2stack4 = p2stack4;
	}
	public List<CurrentCard> getP2stack5() {
		return p2stack5;
	}
	public void setP2stack5(List<CurrentCard> p2stack5) {
		this.p2stack5 = p2stack5;
	}
	public BigRational getP2power() {
		return p2power;
	}
	public void setP2power(BigRational p2power) {
		this.p2power = p2power;
	}
	public BigRational getP2dominance() {
		return p2dominance;
	}
	public void setP2dominance(BigRational p2dominance) {
		this.p2dominance = p2dominance;
	}
	public BigRational getP2strength() {
		return p2strength;
	}
	public void setP2strength(BigRational p2strength) {
		this.p2strength = p2strength;
	}
	public String getTurn() {
		return turn;
	}
	public void setTurn(String turn) {
		this.turn = turn;
	}
	public String getRound() {
		return round;
	}
	public void setRound(String round) {
		this.round = round;
	}
	public String getObjectState() {
		return objectState;
	}
	public void setObjectState(String objectState) {
		this.objectState = objectState;
	}

	public void sqlGetAll() {
		final String URL = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ this.gameID;
		new Thread(new Runnable() {
			public void run() {
				try {
					setObjectState("working");
					HttpClient httpclient = new DefaultHttpClient();
					// Prepare a request object
					HttpGet httpget = new HttpGet(URL);
					// Execute the request
					HttpResponse response;
					response = httpclient.execute(httpget);
					// Get hold of the response entity
					HttpEntity entity = response.getEntity();
					// If the response does not enclose an entity, there is no
					// need
					// to worry about connection release
					if (entity != null) {
						// A Simple JSON Response Read
						InputStream instream = entity.getContent();
						String result = NetUtils
								.convertStreamToString(instream);
						// A Simple JSONObject Creation
						JSONObject json = new JSONObject(result);
						List<String> cardIdList = Arrays.asList(json.getString(
								"p1playDeck").split(","));
						for (int i = 0; i < cardIdList.size(); i++) {
							CurrentCard currentCardObj = new CurrentCard(
									Integer.parseInt(cardIdList.get(i)));
							currentCardObj.copyCardDataFromCardList();
							p1playDeck.add(currentCardObj);
						}
						// Closing the input stream will trigger connection
						// release
						setObjectState("free");
						instream.close();
						httpclient.getConnectionManager().shutdown();
					}
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();

	}
}
